package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author syl
 * @since 2024-07-28
 */
@Service
@Slf4j
public class CategoryServiceImpl  implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     * @param category
     */
    public void save(Category category) {
//        category.builder()
//                .updateUser(BaseContext.getCurrentId())
//                .createUser(BaseContext.getCurrentId())
//                .createTime(LocalDateTime.now())
//                .updateTime(LocalDateTime.now())
//                .status(StatusConstant.DISABLE)
//                .build();

        //公共字段填充替换掉的代码
//        category.setStatus(StatusConstant.DISABLE);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        log.info("新增分类：{}", category);
        categoryMapper.insert(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        log.info("分页查询结果：{}", page);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 修改分类
     * @param category
     */
    public void update(Category category) {
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }


    /**
     * 启用和禁用分类
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        categoryMapper.update(category);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    public Result<String> delete(Long id) {
        Integer isExist = dishMapper.isExistDish(id);
        if (isExist > 0) {
//            return Result.error("该分类下有菜品，不能删除");
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        Integer isExist2 = setmealMapper.isExistSetmeal(id);
        if (isExist2 > 0) {
//            return Result.error("该分类下有套餐，不能删除");
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        if (categoryMapper.getById(id) == 0) {
            return Result.error("分类不存在");
        }


        categoryMapper.deleteById(id);
        return null;
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    public List<Category> list(String type) {
        List<Category> list = categoryMapper.list(type);
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public Category getCategoryById(Long categoryId) {
        Category category = categoryMapper.getCategoryById(categoryId);
        return category;
    }
}
